package com.ww.learn.generic

fun main() {
    // 协变允许子类型关系传递
    val puppyProducer: Producer<Puppy> = PuppyProducer()
    val dogProducer: Producer<Dog> = puppyProducer // 协变 Puppy -> Dog
    val animalProducer: Producer<Animal> = dogProducer // 协变 Dog -> Animal

    val animal: Animal = animalProducer.produce()
    println("Produced: ${animal.name}") // Produced: Tiny

    val puppies: List<Puppy> = listOf(Puppy("P1"), Puppy("P2"), Puppy("P3"))
    val dogs: List<Dog> = puppies
    val animals: List<Animal> = dogs

//    Tiny woof!
//    Tiny woof!
//    Tiny woof!
    animals.forEach { println(it.makeSound()) }
}